package site.teamo.biu.net.client.core.http_https_proxy.mode;

import io.netty.channel.Channel;
import io.netty.handler.codec.http.HttpRequest;
import site.teamo.biu.net.client.core.http_https_proxy.ConnectionHandler;
import site.teamo.biu.net.client.core.http_https_proxy.client.DirectTransferClient;
import site.teamo.biu.net.common.constant.ConnectionType;
import site.teamo.biu.net.common.http_https_proxy.ChannelGroups;
import site.teamo.biu.net.common.http_https_proxy.ConnectionState;
import site.teamo.biu.net.client.core.http_https_proxy.TrafficStatistics;
import site.teamo.biu.net.common.util.HostPort;


/**
 * @author haocongshun
 * @date 2023/06/13 14:51:13
 */
public class DirectConnectionHandler implements ConnectionHandler {

    private final DirectTransferClient directTransferClient;

    public DirectConnectionHandler(TrafficStatistics trafficStatistics) {
        this.directTransferClient = new DirectTransferClient(trafficStatistics.getDirectIn(), trafficStatistics.getDirectOut());
    }

    @Override
    public void http(HostPort hostPort, Channel channelFromBrowser, HttpRequest request) {
        directTransferClient.http(hostPort, channelFromBrowser, request);
    }

    @Override
    public void https(HostPort hostPort, Channel channelFromBrowser) {
        directTransferClient.https(hostPort, channelFromBrowser);
    }

    @Override
    public void close() {
        this.directTransferClient.close();
    }

    @Override
    public ConnectionState directConnectionState() {
        ChannelGroups channelGroups = directTransferClient.getChannelGroups();
        ConnectionState connectionState = new ConnectionState();
        channelGroups.forEach((key, value) -> connectionState.add(key, ConnectionType.DIRECT, value.size(), value.getTimestamp()));
        return connectionState;
    }

    @Override
    public ConnectionState proxyConnectionState() {
        return new ConnectionState();
    }
}
